package lesson7.pages;


import lesson5.ReportWriter;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;

public class HomePage extends BasePage {
    protected static By fieldSearch = By.className("header-search-input-text");
    protected static By searchButton = By.className("btn-link-i");
    protected static By product = By.xpath("//*[@id=\"head_banner_container\"]/div[3]/div/div[1]/div/div/div[3]/div/div[2]/div[1]/a");

    // ****************** Menu elements for Test Menu *************************************
    protected static By computers = By.className("m-main-title");
    protected static By computersValidation = By.className("m-cat-subl-i-link");
    protected static By phones = By.xpath(".//span[contains(text(),'MP3, GPS')]");
    protected static By phonesValidation = By.xpath("//*[@id=\"head_banner_container\"]/div[3]/div/div/div[2]/div[3]/ul/li[1]/ul/li[1]/a");
    protected static By photoVideo = By.xpath("//*[@id=\"tv-photo-video\"]/div/a");
    protected static By photoVideoValidation = By.xpath("//*[@id=\"head_banner_container\"]/div[3]/div/div/div[2]/div[3]/ul/li[1]/ul/li[1]/a");
    protected static By homeAppliance = By.xpath("//*[@id=\"bt\"]/div/a");
    protected static By homeApplianceValidation = By.xpath("//*[@id=\"head_banner_container\"]/div[4]/div/div/div[2]/div[3]/ul/li[1]/h4/a");
    protected static By tourism = By.xpath("//*[@id=\"outdoorsman\"]/div/a");
    protected static By tourismValidation = By.xpath("//*[@id=\"head_banner_container\"]/div[4]/div/div/div[2]/div[3]/ul/li[1]/h4/a");
    protected static By kids = By.xpath("//*[@id=\"kids\"]/div/a");
    protected static By kidsValidation = By.xpath("//*[@id=\"head_banner_container\"]/div[4]/div/div/div[2]/div[3]/ul/li[1]/h4/a");
    protected static By houseGarden = By.xpath("//*[@id=\"house_garden\"]/div/a");
    protected static By houseGardenValidation = By.xpath("//*[@id=\"head_banner_container\"]/div[4]/div/div/div[2]/div[3]/ul/li[1]/h4/a");
    protected static By carGoods = By.xpath("//*[@id=\"273290\"]/div/a");
    protected static By carGoodsValidation = By.xpath("//*[@id=\"head_banner_container\"]/div[3]/div/div/div[2]/div[3]/ul/li[1]/h4/a");
    protected static By travel = By.xpath("//*[@id=\"fatmenu_14\"]/div/a");
    protected static By travelValidation = By.xpath("//*[@id=\"start_search\"]/a");

    // ****************** Online order for Test Order**************************************
    protected static By buyButton = By.name("topurchases");
    protected static By formOrderButton = By.className("bg-r");
    protected static By nameField = By.name("user[recipient_title]");
    protected static By cityField = By.xpath("//*[@id=\"step_contacts\"]/div/div[1]/div[1]/div[2]/div[2]/div/div/div[2]/a");
    protected static By phoneField = By.name("user[phone][title]");
    protected static By userEmailField = By.name("user[email]");
    protected static By editOrderLink = By.name("edit");
    protected static By quantityField = By.name("quantity");
    protected static By recount = By.name("recount");
    protected static By cart = By.className("m-cart-empty");

    public HomePage(WebDriver driver) {
        super(driver);
    }

    public static void search(String searchQuery) {
        driver.findElement(fieldSearch).sendKeys(searchQuery);
        ReportWriter.logElementPresent("search field");
        driver.findElement(searchButton).click();
        ReportWriter.logElementPresent("search button");
        driver.findElement(product).click();
        ReportWriter.info("Product was found");
    }



    public void menuElements() {
        checkMenuElements(computers, computersValidation, "computers menu", "Computers page confirmed");
        checkMenuElements(phones, phonesValidation, "phones menu", "Phones page confirmed");
        checkMenuElements(photoVideo, photoVideoValidation, "photo-video menu", "Photo-video page confirmed");
        checkMenuElements(homeAppliance, homeApplianceValidation, "home appliance menu", "home appliance page confirmed");
        checkMenuElements(tourism, tourismValidation, "tourism menu", "tourism page confirmed");
        checkMenuElements(kids, kidsValidation, "kids menu", "kids page confirmed");
        checkMenuElements(houseGarden, houseGardenValidation, "house-garden menu", "house-garden page confirmed");
        checkMenuElements(carGoods, carGoodsValidation, "cargoods menu", "cargoods page confirmed");
        checkMenuElements(travel, travelValidation, "travel menu", "travel page confirmed");
    }

    private void checkMenuElements(By menuElement,
                                   By menuElemValidation,
                                   String menuItem,
                                   String menuItemConfirmation) {

        driver.findElement(menuElement).click();
        ReportWriter.logElementPresent(menuItem);
        driver.findElement(menuElemValidation);
        ReportWriter.info(menuItemConfirmation);

    }

    public void doOnlineOrder() {
        try {
            driver.findElement(buyButton).click();
            ReportWriter.logElementPresent("buy button");
        } catch (NoSuchElementException e) {
            ReportWriter.logElementNotPresent("buy button");
        }
        try {
            driver.findElement(formOrderButton).click();
            ReportWriter.logElementPresent("form order button");
        } catch (NoSuchElementException e) {
            ReportWriter.logElementNotPresent("form order button");
        }
    }

    public void fillPersonalData(String name, String phoneNumber, String email) {
        driver.findElement(nameField);
        ReportWriter.logElementPresent("name field");
        driver.findElement(nameField).clear();
        driver.findElement(nameField).sendKeys(name);

        driver.findElement(cityField).click();
        ReportWriter.logElementPresent("city field");

        driver.findElement(phoneField);
        ReportWriter.logElementPresent("phone field");
        driver.findElement(phoneField).clear();
        driver.findElement(phoneField).sendKeys(phoneNumber);

        driver.findElement(userEmailField);
        ReportWriter.logElementPresent("email field");
        driver.findElement(userEmailField).clear();
        driver.findElement(userEmailField).sendKeys(email);
    }

    public void editGoodsQuantity() {
        try {
            driver.findElement(editOrderLink).click();
            ReportWriter.logElementPresent("edit order link");
        } catch (NoSuchElementException e) {
            ReportWriter.logElementNotPresent("edit order link");
        }
        driver.findElement(quantityField);
        driver.findElement(quantityField).clear();
        driver.findElement(quantityField).sendKeys("0");
        try {
            driver.findElement(recount).click();
            ReportWriter.info("recounting quantity...");
        } catch (NoSuchElementException e) {
            ReportWriter.logElementNotPresent("recount");
        }
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        try {
            driver.findElement(cart);
            ReportWriter.info("cart is empty");
        } catch (NoSuchElementException e) {
            ReportWriter.logElementNotPresent("cart");
        }
    }
}